package com.novel.service;

import com.novel.common.exception.CustomerException;
import com.novel.common.pojo.RedisParamKey;
import com.novel.mapper.SysLogMapper;
import com.novel.pojo.system.ManageParam;
import com.novel.common.vo.SystemParamVo;
import com.novel.mapper.DownLoadInfoMapper;
import com.novel.mapper.OrderMapper;
import com.novel.pojo.system.DownLoadInfo;
import com.novel.pojo.system.Order;
import com.novel.utils.FormatDateZone;
import com.novel.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service
public class ManagerService {

    @Autowired
    private DownLoadInfoMapper downLoadInfoMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysLogMapper sysLogMapper;

    /**
     * 计算各金额充值数
     */
    public List<Map<String, Object>> getOrderCostMap() {
        return orderMapper.calculateCostAndOrder();
    }


    /**
     * 计算系统访问
     */
    public List<Map<String, Object>> getSystemPowerMap() {
        return sysLogMapper.getSystemPowerMap();
    }

    /**
     * 获取管理员可以修改的系统参数
     */
    public ManageParam getManageParamVo() {

        Properties prop = new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

        try {
            prop.load(new FileInputStream(path + "/manager.properties"));
        } catch (IOException e) {
            throw new CustomerException("读取系统参数异常");
        }

        ManageParam param = new ManageParam();
        param.setMonthRecommendCount(Integer.parseInt(prop.getProperty("manager.month.user.commend.count")));
        param.setAutoPassFlag(Boolean.parseBoolean(prop.getProperty("manager.job.pass.apply.author")));

        return param;
    }

    /**
     * 修改管理员可以修改的系统参数
     */
    public boolean updateManageParam(ManageParam param) {

        Properties prop = new Properties();
        //实现动态加载和更新的方式
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

        try {
            prop.load(new FileInputStream(path + "/manager.properties"));
        } catch (IOException e) {
            throw new CustomerException("读取系统参数异常");
        }


        if (Objects.isNull(param.getAutoPassFlag())) {
            param.setAutoPassFlag(Boolean.FALSE);
        }

        if (Objects.isNull(param.getMonthRecommendCount())) {
            param.setMonthRecommendCount(0);
        }


        try (FileOutputStream fileOutputStream = new FileOutputStream(path + "/manager.properties")) {
            prop.setProperty("manager.job.pass.apply.author", String.valueOf(param.getAutoPassFlag()));
            prop.setProperty("manager.month.user.commend.count", param.getMonthRecommendCount().toString());
            prop.store(fileOutputStream, "修改配置文件,时间：" + DateFormatUtils.format(new Date(), FormatDateZone.Char_Seconds));
        } catch (IOException e) {
            throw new CustomerException("保存系统参数异常");
        }

        return true;
    }


    /**
     * 获取系统参数
     */
    public SystemParamVo getSystemParamInfo() {

        Example orderCountExample = new Example(Order.class);
        orderCountExample.createCriteria().andEqualTo("status", 1);
        int orderCount = orderMapper.selectCountByExample(orderCountExample);


        Example todayCountExample = new Example(Order.class);
        Date now = new Date();
        Date begin = DateUtils.truncate(now, Calendar.DATE);
        todayCountExample.createCriteria()
                .andBetween("createdTime", now, begin)
                .andEqualTo("status", 1);
        int todayCount = orderMapper.selectCountByExample(todayCountExample);


        Example downloadCountExample = new Example(DownLoadInfo.class);
        int downLoadCount = downLoadInfoMapper.selectCountByExample(downloadCountExample);


        return SystemParamVo.builder()
                .onlineCount(redisUtils.stringGet(RedisParamKey.onlineCount))
                .todayOrder(String.valueOf(todayCount))
                .orderCount(String.valueOf(orderCount))
                .downloadCount(String.valueOf(downLoadCount))
                .build();
    }

}